package com.smomo.blog.config.satoken;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import com.smomo.blog.mapper.UserInfoMapper;
import com.smomo.blog.mapper.UserMapper;
import com.smomo.blog.utils.IpUtil;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Component
@RequiredArgsConstructor
public class MySaTokenListener implements SaTokenListener {
    private static final Logger logger = LoggerFactory.getLogger(MySaTokenListener.class);

    public static final List<OnlineUser> ONLINE_USERS = new CopyOnWriteArrayList<>();

    private final UserMapper userMapper;
    private final UserInfoMapper userInfoMapper;

//    @PostConstruct
//    public void init() {
//        initRefreshThread();
//    }

    /** 每次登录时触发 */
    @Override
    public void doLogin(String loginType, Object loginId, SaLoginModel loginModel) {
//        //修改登录信息
//        String ip = IpUtil.getIp();
//        String cityInfo = IpUtil.getCityInfo(ip);
//        UserAgent userAgent = IpUtil.getUserAgent(IpUtil.getRequest());
//        userMapper.updateLoginInfo(loginId,ip,cityInfo,userAgent.getOperatingSystem().getName(),userAgent.getBrowser().getName());
//        //暂时使用内存方式存储在线用户信息
//        String token = StpUtil.getTokenValueByLoginId(loginId);
//        UserInfo userInfo = userInfoMapper.getByUserId(loginId);
//        ONLINE_USERS.add(OnlineUser.builder()
//                .avatar(userInfo.getAvatar())
//                .ip(ip)
//                .city(cityInfo)
//                .loginTime(DateUtil.getNowDate())
//                .os(userAgent.getOperatingSystem().getName())
//                .userId(loginId.toString())
//                .tokenValue(token)
//                .nickname(userMapper.getById(loginId).getNickname())
//                .browser(userAgent.getBrowser().getName()).build());
        logger.info("用户已登录,useId:{},token:{}", loginId,loginModel);
    }

    /** 每次注销时触发 */
    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {
//        ONLINE_USERS.removeIf(onlineUser ->
//                onlineUser.getTokenValue().equals(tokenValue)
//        );
        logger.info("用户已注销,useId:{},token:{}", loginId, tokenValue);
    }

    /** 每次被踢下线时触发 */
    @Override
    public void doKickout(String loginType, Object loginId, String tokenValue) {
//        ONLINE_USERS.removeIf(onlineUser ->
//                onlineUser.getTokenValue().equals(tokenValue)
//        );
        logger.info("用户已踢下线,useId:{},token:{}", loginId, tokenValue);
    }

    /** 每次被顶下线时触发 */
    @Override
    public void doReplaced(String loginType, Object loginId, String tokenValue) {
//        ONLINE_USERS.removeIf(onlineUser ->
//                onlineUser.getTokenValue().equals(tokenValue)
//        );
        logger.info("用户已顶下线,useId:{},token:{}", loginId, tokenValue);
    }

    /** 每次被封禁时触发 */
    @Override
    public void doDisable(String loginType, Object loginId, long disableTime) {
        // ...
    }

    /** 每次被解封时触发 */
    @Override
    public void doUntieDisable(String loginType, Object loginId) {
        // ...
    }

    /** 每次创建Session时触发 */
    @Override
    public void doCreateSession(String id) {
        // ...
    }

    /** 每次注销Session时触发 */
    @Override
    public void doLogoutSession(String id) {
        // ...
        logger.info("user doLogoutSession,id:{}", id);
    }

}
